﻿namespace rtgen_guiclient
{
    using System;
    using System.Text.RegularExpressions;

    public class CHash
    {
        private ulong[] m_aIndices;
        private uint m_nHashID;
        private STATUS m_nStatus;
        private string m_sHash;
        private string m_sHashRoutine;
        private string m_sSalt;
        private stTableSet m_sTableSet;
        private string m_sUsername;

        public CHash(string sHashRoutine, string sHash, string sTableSet, uint nHashID, STATUS nStatus, string sUsername, string sSalt)
        {
            this.m_sHashRoutine = sHashRoutine;
            this.m_sHash = sHash;
            if (sTableSet != "")
            {
                this.m_sTableSet = this.SplitTableSet(sTableSet);
            }
            this.m_nHashID = nHashID;
            this.m_nStatus = nStatus;
            this.m_sSalt = sSalt;
            this.m_sUsername = sUsername;
        }

        public CHash(string sHashRoutine, string sHash, string sCharset, uint nChainLen, uint nMinLetter, uint nMaxLetter, uint nHashID, STATUS nStatus, string sUsername, string sSalt, uint nIndex)
        {
            this.m_sHashRoutine = sHashRoutine;
            this.m_sHash = sHash;
            this.m_sTableSet = new stTableSet();
            this.m_sTableSet.nChainLen = nChainLen;
            this.m_sTableSet.nMinLetter = nMinLetter;
            this.m_sTableSet.nMaxLetter = nMaxLetter;
            this.m_sTableSet.sCharset = sCharset;
            this.m_sTableSet.nIndex = nIndex;
            this.m_nHashID = nHashID;
            this.m_nStatus = nStatus;
            this.m_sSalt = sSalt;
            this.m_sUsername = sUsername;
        }

        public void AddIndices(ulong[] aIndices)
        {
            if (aIndices.Length != (this.m_sTableSet.nChainLen - 1))
            {
                throw new Exception("Indice array is not big enough");
            }
            this.m_aIndices = aIndices;
            this.m_nStatus = STATUS.STATUS_FINISHED;
        }

        public void Assign()
        {
            this.m_nStatus = STATUS.STATUS_ASSIGNED;
        }

        public string GetHash()
        {
            return this.m_sHash;
        }

        public string GetHashRoutine()
        {
            return this.m_sHashRoutine;
        }

        public uint GetID()
        {
            return this.m_nHashID;
        }

        public string GetIndices()
        {
            if (this.m_aIndices == null)
            {
                return "";
            }
            string str = "";
            for (int i = 0; i < this.m_aIndices.Length; i++)
            {
                if (i != 0)
                {
                    str = str + ",";
                }
                str = str + this.m_aIndices[i].ToString();
            }
            return str;
        }

        public string GetSalt()
        {
            return this.m_sSalt;
        }

        public STATUS GetStatus()
        {
            return this.m_nStatus;
        }

        public stTableSet GetTableSet()
        {
            return this.m_sTableSet;
        }

        public string GetUsername()
        {
            return this.m_sUsername;
        }

        public string GetUserName()
        {
            return "";
        }

        public bool IsFinished()
        {
            return (this.m_nStatus == STATUS.STATUS_FINISHED);
        }

        public bool IsNotAssigned()
        {
            return (this.m_nStatus == STATUS.STATUS_NOTASSIGNED);
        }

        private stTableSet SplitTableSet(string sTableSet)
        {
            Match match = new Regex("([a-z-]+) ([1-9]+)-([0-9]+) ([0-9]+)").Match(sTableSet);
            CaptureCollection captures = match.Groups[1].Captures;
            stTableSet set = new stTableSet();
            set.sCharset = match.Groups[1].Captures[0].Value;
            set.nMinLetter = Convert.ToUInt32(match.Groups[2].Captures[0].Value);
            set.nMaxLetter = Convert.ToUInt32(match.Groups[3].Captures[0].Value);
            set.nChainLen = Convert.ToUInt32(match.Groups[4].Captures[0].Value);
            return set;
        }
    }
}

